// pages/person/persondetail/persondetail.js
import Tips from '../../../class/methods/Tips.js'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {

    name: '',
    tel: '',
    uid: ''
  },
  getname: function(e) {
    let that = this
    that.setData({
      name: e.detail.value
    })
  },
  submit() {
    let that = this
    wx.request({
      url: app.globalData.api + "setusername",
      data: {
        uid: that.data.uid,
        username: that.data.name
      },
      success: function(res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '修改成功',
          })
          that.onLoad()
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        } else {
          wx.showToast({
            title: '修改失败',
          })
        }
      },
    })
  },



  // 头像修改函数
  headImg: function() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要修改头像吗',
      success: function(res) {
        if (res.confirm) {
          that.headUpdate()
        } else if (res.cancel) {

        }
      }
    })
  },

  headUpdate: function() {
    let that = this
    // let user_id = wx.getStorageSync('user_id')
    // let userInfo = this.data.userInfo
    Tips.loading()
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // console.log(res.tempFilePaths)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths
        console.log('sss', tempFilePaths[0])
        wx.uploadFile({
          url: app.globalData.api + 'setavatarurl',
          filePath: tempFilePaths[0],
          name: 'img',
          method: "post",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          formData: {
            uid: that.data.uid,
          },
          success: function(res) {
            console.log(res)
            Tips.loaded()
            console.log('修改头像结果', res)
            let data = JSON.parse(res.data)
            console.log(data)
            if (data.code == 0) {
              // userInfo.avatarurl = tempFilePaths[0]
              // that.setData({
              //   userInfo: userInfo
              // })
              Tips.toast('头像修改成功')
              that.onShow()
            } else {
              Tips.alert('头像修改失败')

            }
          },
          fail: function() {
            return false;
          }
        })

      },
      fail: function() {
        Tips.loaded()
        return false;
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log(res, res.data.avatarurl)
        if (res.data.username) {

          wx.request({
            url: app.globalData.api + "getuserinfo",
            data: {
              uid: res.data.id
            },
            success: function(res2) {
              console.log('个人信息', res2, res2.data.list)
              let result = res2.data.list
              that.setData({
                tel: result.phone,
                name: result.username,
                uid: result.id,
                ava: result.avatarUrl
              })
            },
          })

        } else {
          wx.request({
            url: app.globalData.api + "getuserinfo",
            data: {
              uid: res.data.id
            },
            success: function(res2) {
              console.log('个人信息', res2, res2.data.list)
              let result = res2.data.list
              that.setData({
                tel: result.phone,
                name: result.nickname,
                uid: result.id,
                ava: result.avatarurl
              })
            },
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})